package com.baicaoyuan.web.controller;

import com.baicaoyuan.web.common.Status;
import com.baicaoyuan.web.common.annotation.MemberAccess;
import com.baicaoyuan.web.common.exception.StateMessage;
import com.baicaoyuan.web.request.BaseRequest;
import com.qiniu.util.Auth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * 功能描述：  七牛帮助类
 *
 * @author Chen
 * 创建时间： 2017/06/02
 * 版权：百草园电子商务
 */
@Api(value = "项目Controller", tags = {"七牛云接口"})
@RestController
@RequestMapping(value = "/qiniu")
public class QiNiuController {

    private static final String tofoDir = "bcy_laizhuanba";
    private static final String url = "http://bcy.laizhuanba.net";

    private static final String accessKey = "jgAC6w7hq5raHo-TUfTM3HQh662Q01TKDdwzy6Hz";
    private static final String secretKey = "fCgxFv0vOaX1HE4hj4ZljG4MZ7-G7BVbwATLFg7V";


    public String getUpToken(String bucketName) {
        Auth auth = Auth.create(accessKey, secretKey);
        return auth.uploadToken(bucketName);
    }


    /**
     * 获取七牛云令牌
     *
     * @param request
     * @return
     */
    @MemberAccess
    @RequestMapping(value = "get_token", method = RequestMethod.POST)
    @ApiOperation(value = "获取七牛云令牌", httpMethod = "POST", response = Status.class, notes = "upload tofo Image")
    public Status uploadCommon(@RequestBody BaseRequest request) {
        String commonToken = getUpToken(tofoDir);
        return new Status(StateMessage.SUCCESS, commonToken);
    }


    /**
     * 七牛云图片地址
     *
     * @param request
     * @return
     */
    @MemberAccess
    @RequestMapping(value = "get_image_rul", method = RequestMethod.POST)
    @ApiOperation(value = "七牛云图片地址", httpMethod = "POST", response = Status.class, notes = "upload tofo Image")
    public Status getQiNiuUrl(@RequestBody BaseRequest request) {
        Status status = new Status(StateMessage.SUCCESS);
        status.setData("url", url);
        return status;
    }

}
